<?php
/**
 * articles_links model
 *
 *
 * @author:		qfox
 * @package:	radiance
 * @copyright:	(c) 2011, alex <qfox@ya.ru> yaroshevich aka alex_ez. All rights reserved
 * @created:	11.05.2011, 14:34
 */

class articles_links extends model {

	protected $table = 'core_articles_links';
	protected $states = 'off,on,dev';

	public function set_state( $id, $state )
	{
		if( strpos( $this ->states, $state ) !== false )
		$this ->db ->exec( sprintf( 'UPDATE `%s` SET state = "%s" WHERE id = %d;', $this ->table, $state, $id ) );
	}
	
	public function random( $q )
	{
		if( $q<=0 ) $q = 1;
		
		$sql = sprintf( "SELECT * FROM %s WHERE state = 'on' ORDER BY priority*rand() DESC LIMIT 0,%d;", $this ->table, $q );
		
		return $this->db->_list( $sql, 'id' );
	}

	public function get_states() {

		return array(
			'off' => __('Off'),
			'on' => __('On'),
			'dev' => __('On for dev'),
			);
	}
}

